// 获取搜索热词数据
myAjax('http://47.97.121.9:3300/getHotkey')
    .then(res => {
        renderHotSearch(res.response.data.hotkey)
    })

// 获取搜索关键词数据
function getKeyWord(key) {
    myAjax(`http://47.97.121.9:3300/getSmartbox?key=${key}`)
        .then(res => {
            if (res.response != null) {
                renderSearchKey(res.response.data);
            }
        })
}

// 渲染搜索热词数据
function renderHotSearch(data) {
    data.forEach((v, k) => {
        if (k < 5) {
            $('.search_hot_list').append(`
                <dd>
                    <a href="/qqmusic2.0/qqmusic/search/search.html?w=${v.k}" class="search_hot_link">
                        <span class="search_hot_number">${k + 1}</span>
                        <span class="search_hot_name">${v.k}</span>
                        <span class="search_hot_listen">${transPlayCount(v.n)}</span>
                    </a>
                </dd>
            `);
        }
    })
}

// 渲染搜索关键词
function renderSearchKey(data) {
    // 渲染歌曲
    $('.remove_li').remove();
    $('.rm_singer_item').remove();
    data.song.itemlist.forEach((v, k) => {
        $('.search_result_sort').eq(0).children('ul').append(`
            <li class="remove_li">
                <a class="search_result_link" target="_blank" href="javascript:;">
                    <span class="search_result_name">
                        <span class="search_result_keyword">${v.name}</span></span>-<span
                        class="search_result_singer">${v.singer}</span></a>
            </li>
        `);
    })
    // 渲染歌手
    data.singer.itemlist.forEach((v, k) => {
        $('.search_result_sort').eq(1).children('ul').append(`
            <li class="rm_singer_item">
                <a class="search_result_link" target="_blank" href="javascript:;">
                    <span class="search_result_name">
                        <span class="search_result_keyword"></span>
                    </span><span class="search_result_singer">${v.name}</span>
                </a>
            </li>
        `);
    })
    // 渲染专辑
    data.album.itemlist.forEach((v, k) => {
        $('.search_result_sort').eq(2).children('ul').append(`
            <li class="rm_singer_item">
                <a class="search_result_link" target="_blank" href="javascript:;">
                    <span class="search_result_name">
                        <span class="search_result_keyword">${v.name}</span>
                    </span> <span class="search_result_singer">${v.singer}</span>
                </a>
            </li>
        `);
    })
    // 渲染MV
    data.mv.itemlist.forEach((v, k) => {
        $('.search_result_sort').eq(3).children('ul').append(`
            <li class="rm_singer_item">
                <a class="search_result_link" target="_blank" href="javascript:;">
                    <span class="search_result_name">
                        <span class="search_result_keyword">${v.name}</span>
                    </span>-<span class="search_result_singer">${v.singer}</span>
                </a>
            </li>
        `);
    })
}

// 播放量转换为 w 为单位
function transPlayCount(x) {
    if (x < 10000) { return x }
    else { return (`${parseFloat(Math.floor(x / 1000)) / 10}万`) }
}

function myAjax(url, data = {}, type = 'get') {
    return new Promise((resolve, reject) => {
        $.ajax({
            type,
            url,
            data,
            dataType: "json",
            success: function (response) {
                resolve(response);
            },
            error(e) {
                console.log(e);
            }
        });
    })
}